package com.cqndt.disaster.device.controller;

import java.util.Map;
import java.util.List;
import java.util.HashMap;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.cqndt.disaster.device.util.Result;
import com.cqndt.disaster.device.vo.TabUserVo;
import com.cqndt.disaster.device.vo.TabBasicVo;
import com.cqndt.disaster.device.vo.TabHumanVo;
import com.cqndt.disaster.device.domain.TabAttachment;
import com.cqndt.disaster.device.util.JsonArrayToList;
import com.cqndt.disaster.device.domain.TabBasicWithBLOBs;
import com.cqndt.disaster.device.service.TabBasicService;
import org.springframework.web.bind.annotation.PostMapping;
import com.cqndt.disaster.device.service.TabAttachmentService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lgb
 * @date 2020-8-6
 * @说明 Description:灾害点管理
 **/
@RestController
@RequestMapping("/tabBasic/")
public class TabBasicController extends BaseController{
	
    @Autowired
    private TabBasicService tabBasicService;
    @Autowired
    private TabAttachmentService tabAttachmentService;
    
    @PostMapping(value="getTabBasic")
    public Result getTabBasic(Integer id){
        Result result = new Result();
        TabBasicVo vo = tabBasicService.selectTabBasicById(id);
        result.setData(vo);
        return result;
    }
    /**
     * @param disType 灾害点类型
     * @param sqlStr json条件
     * @param areaCode 区域编号
     * @param level 区域等级
     * @return 地图栽点
     **/
    @PostMapping(value="pointTabBasic")
    public Result pointTabBasic(String disType,String sqlStr,
    		String areaCode,String level){
        TabBasicVo vo = new TabBasicVo();
        TabUserVo userVo = getCurrentUser();
        vo.setLevel(null == level || level ==""? userVo.getLevel():level);
        vo.setAreaCode(null == level || level ==""? userVo.getAreaCode():areaCode);
        vo.setDisType(disType);
        vo.setSqlStr(JsonArrayToList.parseJSON2List(sqlStr));
        Result result = new Result();
        List<TabBasicVo> list = tabBasicService.selectTabBasic(vo);
        result.setData(list);
        return result;
    }
    /**
     * @param disType
     * @param sqlStr
     * @param page
     * @param limit
     * @param areaCode
     * @param level
     * @return 灾害点列表
     **/
    @PostMapping(value="listTabBasic")
    public Result listTabBasic(String disType,String sqlStr,Integer page,
    		Integer limit,String areaCode,String level){
        TabBasicVo vo = new TabBasicVo();
        TabUserVo userVo = getCurrentUser();
        vo.setLevel(null == level || level ==""? userVo.getLevel():level);
        vo.setAreaCode(null == level || level ==""? userVo.getAreaCode():areaCode);
        vo.setDisType(disType);
        vo.setSqlStr(JsonArrayToList.parseJSON2List(sqlStr));
        PageHelper.startPage(page,limit);
        PageInfo<TabBasicVo> pageInfo = new PageInfo<TabBasicVo>(tabBasicService.selectTabBasic(vo));
        Result result = new Result();
        result.setCount((int)pageInfo.getTotal());
        result.setData(pageInfo.getList());
        return result;
    }
    
    @PostMapping(value="getAttachmentForBasic")
    public Result getVideoForBasic(Integer id){
        Result result = new Result();
        TabBasicWithBLOBs tabBasicWithBLOBs = tabBasicService.selectById(id);
        List<TabAttachment> imgs = tabAttachmentService.selectAttachments(tabBasicWithBLOBs.getImgIds());
        List<TabAttachment> videos = tabAttachmentService.selectAttachments(tabBasicWithBLOBs.getVideoIds());
        Map<String,Object> map = new HashMap<>();
        map.put("imgs",imgs);
        map.put("videos",videos);
        result.setData(map);
        return result;
    }
    /**
     * @param disNo 灾害点编号
     * @return 获取人员信息
     **/
    @PostMapping(value="getHumanByDisNo")
    public Result getHumanByDisNo(String disNo){
        Result result = new Result();
        List<TabHumanVo> humanList=tabBasicService.selectHumanByDisNo(disNo);
        result.setData(humanList);
        return result;
    }
}